// pages/leaveWord/leaveWord.js
var app=getApp();
var main = require('../../utils/main.js');
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    userInfo: '',
    replyList: [],
    isAdd:true,
    inputFocus:false,
    otherSideId:'',
    replyObj:{},
    reply:'',
    status:'',
    leaveUserId:'',
    pulishId:'',
    backupA:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    main.changeInput.apply(this, []);
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      otherSideId: options.id,
      status: options.status,
      leaveUserId: options.userId,
      pulishId: options.pulishId,
      backupA: options.backupA
    })
    this.getLeaveMes();
  },
  getLeaveMes(flag) {//获取留言数据
    var _this = this;
    var param = {type: 1,};
    if (this.data.status == '8') {
      param.infoId = this.data.otherSideId;
    }else{
      if (this.data.status !== '1') {
        param.infoId = this.data.otherSideId;
        if (this.data.backupA != this.data.userInfo.id) {
          param.userId = this.data.userInfo.id;
          param.otherSideId = this.data.leaveUserId;
        }
      } else {
        param.otherSideId = this.data.userInfo.id;
        if (this.data.backupA != this.data.userInfo.id) {
          param.userId = this.data.userInfo.id;
          param.otherSideId = this.data.leaveUserId;
        }
      }
    }
    wx.request({
      url: app.globalData.httpApiCar + 'messageInfo/all', //仅为示例，并非真实的接口地址
      data: param,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var result = res.data.data;
        result.forEach(function (ele) {
          ele.time = '';
          if (ele.createTime) {
            // ele.time = util.getHours(new Date(ele.create_date.replace(/-/g, "/")).getTime());
            ele.time = util.getHours(ele.createTime);
          }
          if (ele.messageInfos.length > 0) {
            ele.messageInfos = ele.messageInfos.reverse();
            ele.messageInfos.forEach(function (item) {
              if (item.createTime) {
                item.time = util.getHours(item.createTime);
              }
            })
          }
        })
        _this.setData({
          replyList: result
        })
      }
    })
  },
  addLeaveMes() {//留言发送按钮点击事件
    if (this.data.reply!=''){
    var formData = {
      userId: this.data.userInfo.id,
      otherSideId: this.data.replyList[0].userInfo.id,
      message: this.data.reply,
      status: this.data.status,
      backupA: this.data.backupA
    };
    if (this.data.status!=='1'){
      formData.infoId = this.data.replyList[0].infoId;
    }
    if (this.data.isAdd) {
      formData.type = 1;
    } else {
      formData.type = 2;
      formData.otherSideId = this.data.replyObj.userId;
      if (this.data.replyObj.parentId) {
        formData.parentId = this.data.replyObj.parentId;
      } else {
        formData.parentId = this.data.replyObj.id;
      }
    }
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'messageInfo/save', //仅为示例，并非真实的接口地址
      data: formData,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          that.setData({
            page: 1,
            reply: '',
            isAdd:true
          })
          that.getLeaveMes(true);
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    }
  },
  // 
  inputBlur: function (e) {
    this.setData({
      isAdd: true,
      reply: ''
    })
  },
  // 回复按钮点击事件
  reply: function (e) {
    this.data.replyObj = e.currentTarget.dataset.item;
    this.data.replyObj.index = e.currentTarget.dataset.index;
    this.setData({
      isAdd: false,
      inputFocus: true,
      replyObj: this.data.replyObj,
      reply: ''
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})